/**
 * File: cs3343-Satir-Therapy/src/program/CardProcessor.java
 *  
 * Responsible author: Lai Hing Hung, Dennis					Revision no: 1.0.0
 * Checked in at date: 2012/02/05 18:32
 * Checked out at date: 2012/02/12 12:55
 * Log: define CardProcessor constructor, getCard(String), selectEvent()
 * 
 * Responsible author: Lai Hing Hung, Dennis					Revision no: 1.1.0
 * Checked in at date: 2012/04/06 17:02
 * Log: define CardProcessor constructor,
 * 		add getCardNo(), getEventCardDecisionMakerID(), getEventCardTargetID(), selectEvent()
 * 		add setCardNo(), setEventCardDecisionMakerID(), setEventCardTargetID()
 * 
 *      
 */

package program;

import java.util.*;

public class CardProcessor {
//	public int selectToolNo(){
//		Random randomGenerator = new Random();
//		int toolNo = randomGenerator.nextInt(4) + 1;
//		return toolNo;
//	}
	private int cardNo;
	
	// CardProcessor constructor
	public CardProcessor(){
		this.cardNo = 1;
	}
	
	public Card getCard(String cardType, Vector<Player> players){
		Random randomGenerator = new Random();
//		int cardNo = randomGenerator.nextInt(9)+1;
		
		Card card;
		if(cardType.equals("MagicToolCard")){
			int MagicNo = randomGenerator.nextInt(6)+1;
			String magicToolName;
			String magicToolContent;
			switch(MagicNo){
				case 1: magicToolName = "MagicCrown"; 
						magicToolContent = "Magic crown makes myself become the decision maker"; 
						break;
				case 2: magicToolName = "MagicRod"; 
						magicToolContent = "Magic rod subsitutes my deicison for the decision-maker's decision"; 
						break;
				case 3: magicToolName = "MagicShield"; 
						magicToolContent = "Magic shield protects myself from deducting my self esteem values"; 
						break;
				case 4: magicToolName = "MagicKey"; 
						magicToolContent="Magic key allows myself to express my feeling such that my self esteem values would not be reduced"; 
						break;
				case 5: magicToolName = "MagicMirror"; 
						magicToolContent="Magic mirror rebounds the loss to the decision maker such that my self esteem values would not be reduced";
						break;
				default: magicToolName = "MagicGlove";
						 magicToolContent="Magic glove allows myself to appoint one family member to complete the task"; 
						 break;
			}
			card= new MagicToolCard(cardNo, magicToolName, magicToolContent);
//			return card;
		}
		else if(cardType.equals("EventCard")){
			int eventNo = selectEvent(players);
			String cardName ="";
			String eventContent ="";
			String decisionMaker ="";
			String target = "";
			
			/**
			 * 	例子1：仔仔（女女）學校的終期考試臨近，父親因為仔仔（女女）在上學期的中文考試成績得到六十分而禁止他／她於星期六出外。
			 *	但是這星期六（女女）的好同學邀請他／她出席生日會。媽媽請決定如何解決這事情？
			 *	A) 應承去，但打算說謊因為其他特別事情，而不能參與。（減2個自尊心值）
			 *	B) 雖然想去但拒絕參與，因爲怕父母不開心而推卻不參與。（減1個自尊心值）
			 *	C) 與父母親討論溫習時間，制定新旳温習時間表，並表達自己的意願。(增加2個自尊心值）
			 *	D) 問兄姊的意見。（增加1個自尊心值）
			 * 
			 */

			if(eventNo == 1){				
				cardName="EventCase1";
				eventContent="The semester examination of Son (daughter)'s school is coming.\n" 
								+ "Father forbids him(her) to go out because he(she) got only 60 marks in Chinese examination.\n"
								+ "But this Saturday his(her) best classmate invites him(her) to attend the birthday party.\n" 
								+ "Please GrandMa/Mother decides how to solve the problem?\n"
								+ "A) You consent to go to the party, but you plan to tell lie that you cannot participate because of other reason\n"
								+ "B) Though you want to participate, you refuse to participate because of make parents unhappy\n"
								+ "C) discuss the study hour with parents, schedule the review time table and express your wish\n"
								+ "D) consult your sibling's opinion"
								;
						
				for(int i=0; i<players.size(); i++){
					if(players.get(i).getRole().equals("GrandMa")){
//						decisionMaker = players.get(i).getName();
						decisionMaker = players.get(i).getID();
					}
							
					if(players.get(i).getRole().equals("Mother")){
//						decisionMaker = players.get(i).getName();
						decisionMaker = players.get(i).getID();
					}
				}
						
//						decisionMaker="Mother";
						
				for(int i=0; i<players.size(); i++){
					if(players.get(i).getRole().equals("Son")){
//						target = players.get(i).getName();
						target = players.get(i).getID();
					}
					
					if(players.get(i).getRole().equals("Daughter")){
//						target = players.get(i).getName();
						target = players.get(i).getID();
					}
				}
						
//						target="Son or Daughter";
				//		break;
			}			
			
			/**
			 * 例子2：媽媽連續一星期內發現仔仔（女女）於晚上與學校的同班同學傾電話長達三十分鐘。爸爸請決定如何解決這事情？
			 * A) 立即責罵。禁止再與同學傾電話達五分鐘以上。（減2個自尊心值）
			 * B) 向仔仔（女女）指出連續一星期傾電話的發現。了解傾電話的原因。溫和地講出自己的感受和影響。期望他們應該有節制。(增加2個自尊心值）
			 * C) 威嚇向爸爸投訴，如不改善這星期將會被扣除零用錢。（減1個自尊心值）
			 * D) 不予理會。（減1個自尊心值）
			 */

			if(eventNo==2){ 
				cardName="EventCase2";
				eventContent="Father discovers that son or daughter is dating.\n" 
					+ "Please GrandMa/Mother decides how to solve the problem?\n"
					+ "A) blame son/daughter and forbid him/her to date further.\n"
					+ "B) tell son or daughter that parents have discovered that he/she is dating. realize the reason and mildly speak out my own feeling and influence. expect hime/her to hold down his/her passion.\n"
					+ "C) threaten to complain son/daughter about the matter with his/her father. His/Her pocket money will be deducted if the situation is not improved.\n"
					+ "D) ignore son/daughter"; 
				
				for(int i=0; i<players.size(); i++){
					if(players.get(i).getRole().equals("GrandMa")){
//						decisionMaker = players.get(i).getName();
						decisionMaker = players.get(i).getID();
					}
					
					if(players.get(i).getRole().equals("Mother")){
//						decisionMaker = players.get(i).getName();
						decisionMaker = players.get(i).getID();
//						break;
					}
				}
				
				for(int i=0; i<players.size(); i++){
					if(players.get(i).getRole().equals("Daughter")){
//						target = players.get(i).getName();
						target = players.get(i).getID();
					}
					
					if(players.get(i).getRole().equals("Son")){
//						target = players.get(i).getName();
						target = players.get(i).getID();
//						break;
					}
				}
//				decisionMaker="Father";
//				target="Son or Daughter";
//				break;
			}
			/***
			 * 例子3：仔仔（女女）因欠交功課，遭媽媽禁止他／她去學校旅行。​請父親決定如何解決這事情？
 			 * A) 應承讓他/她去，但打算說謊因為其他特別事情，而不能讓他/她參與。（減1個自尊​心值）
 			 * B) 雖然想讓仔仔(女女)參與，因爲怕妻子不開心而不讓仔仔(女女)。（減2個自​尊心值）
 			 * C) 與母親討論這件事，並表達自己的意見。(​增加1個自尊心值）
 			 * D) 問仔仔(女女)的感受。（增加2個自尊心值）			
			 */		
			if(eventNo==3){
				cardName="EventCase3";
				eventContent="Son or daughter is forbidden to go to school travel because he/she did not hand in homework.\n" 
					+ "Please GrandPa/Father decides how to solve the problem?\n"
					+ "A) consent to allow him/her to go, but pretend not to ban him/her because of other matters.\n"
					+ "B) Though want to let him/her go but refused to do so because of fear of wife.\n"
					+ "C) discuss the matter with wife and express my opinion. (An increase in self-esteem value)\n"
					+ "D) ask the siblings about their feelings."; 
				
				for(int i=0; i<players.size(); i++){
					if(players.get(i).getRole().equals("GrandPa")){
//						decisionMaker = players.get(i).getName();
						decisionMaker = players.get(i).getID();
					}
					
					if(players.get(i).getRole().equals("Father")){
//						decisionMaker = players.get(i).getName();
						decisionMaker = players.get(i).getID();
//						break;
					}
				}
				
				for(int i=0; i<players.size(); i++){
					if(players.get(i).getRole().equals("Son")){
//						target = players.get(i).getName();
						target = players.get(i).getID();
					}
					
					if(players.get(i).getRole().equals("Daughter")){
//						target = players.get(i).getName();
						target = players.get(i).getID();
//						break;
					}
				}
//				decisionMaker="Father";
//				target="Son or Daughter";
//				break;
			}
			
			/***
			 * 例子4：爸爸發現仔仔（女女）拍拖。媽媽請決定如何解決這事情？
 			 * A) 立即責罵。禁止拍拖。（減3個自尊心值）
 			 * B) 向仔仔（女女）指出父母發現他/她拍拖了。了解原因,溫和地講出自己的感受​和影響。期望他們應該有節制。(增加1個自尊心值）
 			 * C) 威嚇向爸爸投訴，如不改善這星期將會被扣除零用錢。（減2個自尊​心值）
 			 * D) 不予理會。（減1個自尊心值）
			 */
						
			if(eventNo==4){ 
				cardName="EventCase4";
				eventContent="Mother discovered that son(daughter) telephone classmate for half a hour at night for one week.\n" 
					+ "Please GrandPa/Father decides how to solve the problem?\n"
					+ "A) blame him(her) immediately. Stop him(her) from dialogue with classmate more than 5 minutes\n"
					+ "B) point out that you discover the matter. realize the reason behind telephone. speak out your own feelings and influence modestly. expect they should control themselves\n"
					+ "C) threaten to complain to Father, if the problem would not be rectified, pocket money will be deducted\n"
					+ "D) ignore him/her";
				
				for(int i=0; i<players.size(); i++){
					if(players.get(i).getRole().equals("GrandPa")){
//						decisionMaker = players.get(i).getName();
						decisionMaker = players.get(i).getID();
					}
					
					if(players.get(i).getRole().equals("Father")){
//						decisionMaker = players.get(i).getName();
						decisionMaker = players.get(i).getID();
//						break;
					}
				}
				
				for(int i=0; i<players.size(); i++){
					if(players.get(i).getRole().equals("Daughter")){
//						target = players.get(i).getName();
						target = players.get(i).getID();
					}
					
					if(players.get(i).getRole().equals("Son")){
//						target = players.get(i).getName();
						target = players.get(i).getID();
//						break;
					}
				}
				
				
//				decisionMaker="Father";
//				target="Son or Daughter";
//				break;
						
			}
			card= new EventCard(cardNo, cardName, eventContent, decisionMaker, target);
//			return card;
		}
		else{
			int sayAndActionNo = randomGenerator.nextInt(6)+1;
			String sayAndActionName;
			String sayAndActionContent;
			switch(sayAndActionNo){
				case 1: sayAndActionName = "Say something appreciative"; 
						sayAndActionContent = "Speak to your family member with appreciation"; 
						break;
				case 2: sayAndActionName = "Say something encouraging"; 
						sayAndActionContent = "Speak to your family member with encouragement"; 
						break;
				case 3: sayAndActionName = "Say something considerate"; 
						sayAndActionContent = "Show consideration for your family member"; 
						break;
				case 4: sayAndActionName = "Take action of dancing"; 
						sayAndActionContent = "Dance with your family member"; 
						break;
				case 5: sayAndActionName = "Take action of singing"; 
						sayAndActionContent="Sing a song to your family member";
						break;
				default: sayAndActionName = "Take action of hug";
						 sayAndActionContent="Hug your family member"; 
						 break;
			}
			card= new SayAndActionCard(cardNo, sayAndActionName, sayAndActionContent);
//			return card;
		}
		cardNo++;
		return card;
	}
	
	public int selectEvent(Vector<Player> players){
		Random randomGenerator = new Random();
		int eventNo =0;
		
		if(players.size()==2){
			for(int i=0; i<players.size(); i++){
				if(players.get(i).getRole().equals("GrandMa") || players.get(i).getRole().equals("Mother")){
					// event case involves mother decision
					//eventNo = randomGenerator.nextInt(1)+1;
					if(cardNo%2==1){
						eventNo = 1;
					}
					else{
						eventNo = 2;
					}
					return eventNo;
				}
			}
			// event case involves father decision
			//eventNo = randomGenerator.nextInt(1)+3;
			if(cardNo%2==1){
				eventNo = 3;
			}
			else{
				eventNo = 4;
			}
			return eventNo;
		}
		else if(players.size()==3){
			int mother = 0;
			int father = 0;
			for(int i=0; i<players.size(); i++){
				if(players.get(i).getRole().equals("GrandMa") || players.get(i).getRole().equals("Mother")){
					mother++;
				}
				if(players.get(i).getRole().equals("GrandPa") || players.get(i).getRole().equals("Father")){
					father++;
				}
			}
			if(mother==2){
				// event case involves mother decision
//				eventNo = randomGenerator.nextInt(1)+1;
				if(cardNo%2==1){
					eventNo = 1;
				}
				else{
					eventNo = 2;
				}
				return eventNo;
			}
			else if(father==2){
				// event case involves father decision
//				eventNo = randomGenerator.nextInt(1)+3;
				if(cardNo%2==1){
					eventNo = 3;
				}
				else{
					eventNo = 4;
				}
				return eventNo;
			}
			else{
				// event case involves both father and mother decision
				eventNo = randomGenerator.nextInt(4)+1;
				return eventNo;
			}
		}
		else{
			// event case involves both father and mother decision
			eventNo = randomGenerator.nextInt(4)+1;
			return eventNo;
		}
	}
	
	public String getEventCardDecisionMakerID(EventCard eventCard){
		return eventCard.getDecisionMakerID();
	}
	
	public void setEventCardDecisionMakerID(EventCard eventCard, String decisionMakerID){
		eventCard.setDecisionMakerID(decisionMakerID);
	}
	
	public String getEventCardTargetID(EventCard eventCard){
		return eventCard.getEventTargetID();
	}
	
	public void setEventCardTargetID(EventCard eventCard, String targetID){
		eventCard.setEventTargetID(targetID);
	}
	
	public int getCardNo(){
		return cardNo;
	}
	
	public void setCardNo(int cardNo){
		this.cardNo = cardNo;
	}
}
